﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Globalization;

namespace Microsoft.Office.OneNote
{
    [OneNoteXmlElement("Margins")]
    public sealed class OneNotePageMargins : OneNoteObject, IEquatable<OneNotePageMargins>
    {
        private const string ToStringFormat = "{0},{1},{2},{3}";

        internal static readonly OneNoteProperty LeftProperty = OneNoteProperty.Register(
            "left", typeof(Double), typeof(OneNotePageMargins));

        internal static readonly OneNoteProperty TopProperty = OneNoteProperty.Register(
            "top", typeof(Double), typeof(OneNotePageMargins));

        internal static readonly OneNoteProperty RightProperty = OneNoteProperty.Register(
            "right", typeof(Double), typeof(OneNotePageMargins));

        internal static readonly OneNoteProperty BottomProperty = OneNoteProperty.Register(
            "bottom", typeof(Double), typeof(OneNotePageMargins));

        internal OneNotePageMargins()
        {

        }

        public OneNotePageMargins(double left, double top, double right, double bottom)
        {
            SetValue(LeftProperty, left);
            SetValue(TopProperty, top);

            SetValue(RightProperty, right);
            SetValue(BottomProperty, bottom);
        }

        public double Left
        {
            get { return GetValue<Double>(LeftProperty); }
        }

        public double Top
        {
            get { return GetValue<Double>(TopProperty); }
        }

        public double Right
        {
            get { return GetValue<Double>(RightProperty); }
        }

        public double Bottom
        {
            get { return GetValue<Double>(BottomProperty); }
        }

        public override int GetHashCode()
        {
            return Left.GetHashCode() ^ Top.GetHashCode() ^ 
                   Right.GetHashCode() ^ Bottom.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            return this.EqualsHelper(obj);
        }

        public bool Equals(OneNotePageMargins other)
        {
            if (ReferenceEquals(other, null))
            {
                return false;
            }

            return DoubleHelper.AreClose(Left, other.Left) && 
                   DoubleHelper.AreClose(Top, other.Top) &&
                   
                   DoubleHelper.AreClose(Right, other.Right) &&
                   DoubleHelper.AreClose(Bottom, other.Bottom);
        }

        public override string ToString()
        {
            return String.Format(CultureInfo.InvariantCulture, ToStringFormat, Left, Top, Right, Bottom);
        }
    }
}